<?php

namespace App\Jobs;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Services\MakeSeckillOrder\CloseUnpaidSeckillOrder;

class CloseSeckillOrderJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected int $uid;
    protected string $unpaidCacheId;

    public function __construct($uid, $unpaidCacheId)
    {
        $this->uid = $uid;
        $this->unpaidCacheId = $unpaidCacheId;
        $requiredMinutes = cs('good')->destory_time;
        $minutes = app()->environment('local') ? 2 : $requiredMinutes;
        $this->delay(Carbon::now()->addMinutes($minutes));
    }

    public function handle()
    {
        app(CloseUnpaidSeckillOrder::class, [
            'uid' => $this->uid,
            'unpaidCacheId' => $this->unpaidCacheId
        ])->close();
    }
}
